package com.wzy.web.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wzy.web.pojo.PmUser;

/**
 * A data access object (DAO) providing persistence and search support for
 * PmUser entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.shootingchance.pojo.PmUser
 * @author MyEclipse Persistence Tools
 */

public class PmUserDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(PmUserDAO.class);
	// property constants
	public static final String USER_NAME = "userName";
	public static final String USER_PASS = "userPass";
	public static final String USER_PARTMENT = "userPartment";
	public static final String USER_SEX = "userSex";
	public static final String DATE_TIME = "dateTime";

	protected void initDao() {
		// do nothing
	}
	/**
	 * 验证登陆
	 * @param userName
	 * @param userPass
	 * @return
	 */
	public PmUser checkUserLogin(String userName,String userPass) {
		Session session = null;
		List<PmUser> list = null;
		Query q = null;
		String hql = "from PmUser where userName=:userName and userPass=:userPass";
		try {
			session = this.getHibernateTemplate().getSessionFactory().openSession();
			q = session.createQuery(hql).setString("userName", userName).setString("userPass", userPass);
			list = q.list();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		if(list != null && list.size() == 1) {
			return list.get(0);
		} else {
			return null;
		}
	}
	public void save(PmUser transientInstance) {
		log.debug("saving PmUser instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(PmUser persistentInstance) {
		log.debug("deleting PmUser instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public PmUser findById(java.lang.Integer id) {
		log.debug("getting PmUser instance with id: " + id);
		try {
			PmUser instance = (PmUser) getHibernateTemplate().get(
					"com.wzy.web.pojo.PmUser", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<PmUser> findByExample(PmUser instance) {
		log.debug("finding PmUser instance by example");
		try {
			List<PmUser> results = (List<PmUser>) getHibernateTemplate()
					.findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding PmUser instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from PmUser as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<PmUser> findByUserName(Object userName) {
		return findByProperty(USER_NAME, userName);
	}

	public List<PmUser> findByUserPass(Object userPass) {
		return findByProperty(USER_PASS, userPass);
	}

	public List<PmUser> findByUserPartment(Object userPartment) {
		return findByProperty(USER_PARTMENT, userPartment);
	}

	public List<PmUser> findByUserSex(Object userSex) {
		return findByProperty(USER_SEX, userSex);
	}

	public List<PmUser> findByDateTime(Object dateTime) {
		return findByProperty(DATE_TIME, dateTime);
	}

	public List<PmUser> findAll() {
		log.debug("finding all PmUser instances");
		try {
			String queryString = "from PmUser";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public PmUser merge(PmUser detachedInstance) {
		log.debug("merging PmUser instance");
		try {
			PmUser result = (PmUser) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(PmUser instance) {
		log.debug("attaching dirty PmUser instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(PmUser instance) {
		log.debug("attaching clean PmUser instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static PmUserDAO getFromApplicationContext(ApplicationContext ctx) {
		return (PmUserDAO) ctx.getBean("PmUserDAO");
	}
}